import { defineStore } from 'pinia'

export interface Post {
  id: number
  title: string
  description: string
  content?: string
  coverImage: string
  date: string
  tags: string[]
  views: number
  isTop?: boolean
}

export const useBlogStore = defineStore('blog', {
  state: () => ({
    posts: [
      {
        id: 1,
        title: '从零手搭：前后端分离博客（全栈开发，已更新39w+字）',
        description: '已更新 244 节内容，共计 39w+ 字，演示截图：1661 张，2期目前已经完结...',
        coverImage: 'https://ext.same-assets.com/412138514/3527979049.jpeg',
        date: '2023-11-23',
        tags: ['专栏', '知识星球', '项目实战', '从 0 到 1'],
        views: 12500,
        isTop: true
      },
      {
        id: 2,
        title: '从零手搭：小哈兔（仿小红书，微服务架构，已更新45w+字）',
        description: '已更新 33w+ 字，演示截图：1469 张，持续爆肝中，目前核心服务快完结了...',
        coverImage: 'https://ext.same-assets.com/412138514/2204661460.png',
        date: '2024-07-06',
        tags: ['专栏', '知识星球', '项目实战', '从 0 到 1', '微服务'],
        views: 18700,
        isTop: true
      },
      {
        id: 3,
        title: '手摸手带你学会 Elasticsearch 单机、集群、插件安装(图文教程)',
        description: '手把手带你学会 Elasticsearch 单机、集群、插件安装(图文教程)',
        coverImage: 'https://ext.same-assets.com/412138514/164083749.jpeg',
        date: '2024-01-28',
        tags: ['Elasticsearch', '搜索引擎', '图文教程'],
        views: 8320
      },
      {
        id: 4,
        title: 'Windows 系统如何安装 VSCode？（图文教程）',
        description: 'Windows 系统如何安装 VSCode？（图文教程）',
        coverImage: 'https://ext.same-assets.com/412138514/3999251441.png',
        date: '2024-01-22',
        tags: ['工具', '知识星球', '专栏'],
        views: 6240
      },
      {
        id: 5,
        title: 'Java 如何调用执行 Python 脚本？手把手讲解',
        description: 'Java 如何调用执行 Python？图文讲解超详细！',
        coverImage: 'https://ext.same-assets.com/412138514/1050120414.jpeg',
        date: '2023-11-23',
        tags: ['Python', '脚本', '构建言'],
        views: 9870
      },
      {
        id: 6,
        title: 'Postman 汉化教程，手把手教你如何设置中文',
        description: 'Postman 汉化教程，手把手教你如何设置中文',
        coverImage: 'https://ext.same-assets.com/412138514/3960124426.jpeg',
        date: '2023-11-23',
        tags: ['API 调试', '工具', '汉化'],
        views: 7520
      }
    ] as Post[]
  }),
  getters: {
    getPostById: (state) => (id: number) => {
      return state.posts.find(post => post.id === id)
    },
    getTopPosts: (state) => {
      return state.posts.filter(post => post.isTop)
    },
    getRecentPosts: (state) => {
      return [...state.posts].sort((a, b) => {
        return new Date(b.date).getTime() - new Date(a.date).getTime()
      })
    }
  }
})
